import urllib.request
import urllib.parse
import re
import json
import time
i=-1
j=0
bets=[]
request = urllib.request.Request(r"http://toto-info.fonbet.com/DataService.svc/SelectStakesInterval")
request.add_header("Content-Type",r"application/json; charset=UTF-8")
while i<121320:
    tm=time.time()
    data = (r'{"options":{"StartFrom":'+str(i)+r',"Count":9000,"SortField":"CouponCode","SortDir":"DESC","DrawingId":732,"From":30,"To":999000}}').encode('utf-8')
    print(data)
    f = urllib.request.urlopen(request, data)
    print(time.time()-tm)
    resp=f.read().decode('utf-8')
    f.close()
    i+=9000
    j+=1
    p=json.loads(resp)
    for  row in p.get('d').get('Items'):bets.append([row.get('Options')])
    print(i,len(bets))
    print(time.time()-tm)

##my format
num='3'+'7'*8+'8'*6
cbets=[]
for bet in bets:
    sum=0;a=''
    for pos in num:
        sum+=int(pos)
        a+=bet[0][sum]
    cbets.append(a)
##probabalyties
import toto
import re
e=r'<tr><td>(.+)<td>.+<td>(.+)<td class=bl>(\d{2}).+td>(\d{2}).+br>(\d{2})'
l=re.findall(e,open('748.html','r').read())[0:15]
ps=[]
for row in l:
    temp=[]
    for r in row[2:]:temp.append(float(r)/100)
    ps.append(temp)
##gen
for i in range(100):
    bet=toto.genbet(ps).upper()
    cnt=0
    x1=toto.sv(bet,'XXXXXXXXXXXXXXX')
    x2=toto.sv(bet,'111111111111111')
    x3=toto.sv(bet,'211211122X12X1X')
    if (x1 in '234')and (x2 in '6789'):
        for cbet in cbets:
            if toto.sv(bet,cbet)=='14' or toto.sv(bet,cbet)=='13' or toto.sv(bet,cbet)=='15' or toto.sv(bet,cbet)=='12':cnt+=1

    if (x1 in '234')and (x2 in '6789'): print(bet,cnt,x3,'yes')
    elif len(x3)==2:print(bet,cnt,x3,'no')




